<template>
  <div id="app">
    <van-nav-bar
      title="我的积分"
      left-text="返回"
      left-arrow
      @click-left="onClickLeft"
    />
    <van-cell-group>
      <el-row>
        <el-col :span="24">
          <van-badge style="text-align: left">
            <van-icon name="gem-o" />
            <span class="product-points">我的积分: <span class="da">{{ user.score }}</span> 分</span>
            <el-button size="small" class="sx " @click="findUserById">刷新积分</el-button>
          </van-badge>
        </el-col>
      </el-row>
    </van-cell-group>
    <van-tabs v-model="active">
      <van-tab title="积分收支明细">
        <van-list
          v-model="loading"
          :finished="finished"
          finished-text="没有更多了"
          @load="onLoad"
        >
          <van-cell
            v-for="item in pointsList"
            :key="item.date"
            :title="item.name"
            :value="(item.score == 1000 ? '+' : '-') + Math.abs(item.score) + '分'"
            :label="item.datee"
          />
        </van-list>
      </van-tab>
    </van-tabs>

  </div>
</template>

<script>
import axios from "axios";
import {Toast} from "vant";

export default {
  data() {
    return {
      user:{},
      userId: '',
      active: 2,
      loading: false,
      finished: false,
      pointsList: [],
    };
  },
  methods: {
    onClickLeft() {
      Toast('返回');
      //返回上一个页面
      this.$router.push('myShow');
    },
    findUserById() {
      axios.post(`/user/user/findById?id=${this.userId}`).then(res => {
        this.user = res.data.data;
      })
    },
    findUserScore(){
      axios.post(`user/user/findUserScore?id=${this.userId}`).then(res=>{
        this.pointsList = res.data.data.score;

      })
    },
    onLoad() {
      // 模拟加载更多数据
      setTimeout(() => {
        this.loading = false;
        if (this.pointsList.length >= 2) {
          this.finished = true;
        }
      }, 500);
    }
  },
  mounted() {
    var from = localStorage.getItem('user');
    if (from) {
      this.from = JSON.parse(from);
      this.userId = this.from.id;
      console.log("uid=", this.userId);
    }
    this.findUserById();
    this.findUserScore();
  }
};
</script>

<style>
</style>
